package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import CONTROLLERS.KhoaControllers;
import ModelEntityFW.KhoaEntity;
import ModelEntityFW.UserEntitys;
import java.awt.Toolkit;
import javax.swing.JToolBar;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class frmEditKhoa extends JFrame {

	private JPanel contentPane;
	private static Connection m_objConnections;
	private KhoaControllers m_objControllers;
	private static UserEntitys m_objSecurityContext;
	private static KhoaEntity m_objKhoaEntity;
	private JTextField maKhoa;
	private JTextField tenKhoa;
	private JTextField soLuong;
	private JComboBox maNV;

	/**
	 * Launch the application.
	 */
	/*
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { frmEditKhoa frame = new
	 * frmEditKhoa(); frame.setVisible(true); } catch (Exception e) {
	 * e.printStackTrace(); } } }); }
	 */

	/**
	 * Create the frame.
	 */
	public frmEditKhoa(Connection objConnection, UserEntitys objSecurity, KhoaEntity objKhoaEntity) {
		m_objConnections = objConnection;
		m_objSecurityContext = objSecurity;
		m_objKhoaEntity = objKhoaEntity;
		m_objControllers = new KhoaControllers(m_objConnections, m_objSecurityContext);

		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(frmEditKhoa.class.getResource("/images/13.png")));
		setTitle("Chỉnh sửa thông tin Khoa");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 416, 321);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JToolBar toolBar = new JToolBar();
		toolBar.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.setBounds(0, 0, 410, 43);
		contentPane.add(toolBar);

		JButton btnNewButton = new JButton();
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//m_objKhoaEntity.setMaKhoa("" + maKhoa.getText());
				m_objKhoaEntity.setTenKhoa(tenKhoa.getText());
				m_objKhoaEntity.setSoLuong("" + soLuong.getText());
				m_objKhoaEntity.setMaNhanVien("" + (maNV.getSelectedIndex()+1));
				if (m_objKhoaEntity.isbFlag()) {
					m_objKhoaEntity.setMaKhoa("" + maKhoa.getText());
					boolean bResult = false;
					try {
						bResult = m_objControllers.ExecOfUpdateElement(m_objKhoaEntity);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(getParent(), "Lỗi: " + e.getMessage(), "ERROR!!!",
								JOptionPane.ERROR_MESSAGE);
						// TODO: handle exception
					}

					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Cập nhật thành công!!!", "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
				}else{
					boolean bResult = false;
					try {
						bResult = m_objControllers.ExecOfInsertElement(m_objKhoaEntity);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(getParent(), "Lỗi: " + e.getMessage(), "ERROR!!!",
								JOptionPane.ERROR_MESSAGE);
						// TODO: handle exception
					}

					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Thêm mới thành công!!!", "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
				}
				dispose();
			}
		});
		btnNewButton.setIcon(new ImageIcon(frmEditKhoa.class.getResource("/images/3_2.png")));
		btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnNewButton);

		JButton btnng = new JButton("Đóng");
		btnng.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnng.setIcon(new ImageIcon(frmEditKhoa.class.getResource("/images/6_3.png")));
		btnng.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnng);

		JLabel lblNewLabel = new JLabel("Mã khoa");
		lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel.setBounds(21, 70, 73, 18);
		contentPane.add(lblNewLabel);

		JLabel lblTenKhoa = new JLabel("Tên khoa");
		lblTenKhoa.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblTenKhoa.setBounds(21, 120, 83, 18);
		contentPane.add(lblTenKhoa);

		JLabel lblSoLuong = new JLabel("Số lượng");
		lblSoLuong.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblSoLuong.setBounds(21, 170, 83, 18);
		contentPane.add(lblSoLuong);

		JLabel lblMNhnVin = new JLabel("Tên nhân viên");
		lblMNhnVin.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblMNhnVin.setBounds(21, 220, 94, 18);
		contentPane.add(lblMNhnVin);

		maKhoa = new JTextField();
		maKhoa.setEditable(false);
		maKhoa.setBounds(125, 66, 264, 29);
		contentPane.add(maKhoa);
		maKhoa.setColumns(10);

		tenKhoa = new JTextField();
		tenKhoa.setColumns(10);
		tenKhoa.setBounds(125, 116, 264, 29);
		contentPane.add(tenKhoa);

		soLuong = new JTextField();
		soLuong.setColumns(10);
		soLuong.setBounds(125, 166, 264, 29);
		contentPane.add(soLuong);
		
		maNV = new JComboBox(setItemJCC(m_objConnections));
		maNV.setBounds(125, 216, 264, 29);
		contentPane.add(maNV);
		if (m_objKhoaEntity.isbFlag()) {
			btnNewButton.setText("Cập nhật");
			BindingControls(m_objKhoaEntity);
		} else {
			btnNewButton.setText("Thêm mới");
		}

	}
	
	public static Vector<String> setItemJCC(Connection objConnection) {
		CallableStatement objCmst = null;
		ResultSet objRs = null;

		Vector<String> item = new Vector<String>();
		try {
			objCmst = objConnection.prepareCall("{call SP_GET_THONGTIN_NHAN_VIEN()}");

			objRs = objCmst.executeQuery();

			while (objRs.next()) {
				item.addElement(objRs.getString("HoVaTen"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.getMessage();
		}
		return item;
	}
	
	public void BindingControls(KhoaEntity objKhoaEntity) {
		maKhoa.setText("" + objKhoaEntity.getMaKhoa());
		tenKhoa.setText(objKhoaEntity.getTenKhoa());
		soLuong.setText("" + objKhoaEntity.getSoLuong());
		maNV.setSelectedIndex(Integer.parseInt(objKhoaEntity.getMaNhanVien()));
	}
}
